package com.apache.audit.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.manager.LogOperationManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;

public class LogOperationManagerImpl implements LogOperationManager {

	protected IDao logOperationDao;
	protected final String entityName = "com.apache.audit.entity.LogOperation";

	public void setLogOperationDao(IDao logOperationDao) {
		this.logOperationDao = logOperationDao;
	}

	public String saveInfo(ParamsVo<LogOperation> vo) throws BusinessException {
		LogOperation logOperation = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="logOperation_"+infoId;
		if (Validator.isNull(logOperation.getLogId()))
			logOperation.setLogId(infoId);
		MethodParam param = new MethodParam("LogOperation", cacheKey, "", entityName);
		param.setVaule(logOperation);
		if (logOperationDao.insert(param)) {
			return logOperation.getLogId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<LogOperation> vo) throws BusinessException {
		LogOperation logOperation = vo.getObj();
		if (Validator.isNotNull(logOperation.getLogId())) {
			String cacheKey = "";
			//String cacheKey="logOperation_"+logOperation.getLogId();
			MethodParam param = new MethodParam("LogOperation", cacheKey, "", entityName);
			param.setVaule(logOperation);
			return logOperationDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<LogOperation> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="logOperation_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		LogOperation info = (LogOperation) logOperationDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("LogOperation");
			return logOperationDao.edit(param);
		} else {
			param.setParams("logId", infoId);
			param.setDelete(true);
			return logOperationDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<LogOperation> vo) {
		String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="logOperation_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam(key, cacheKey, "", entityName);
		param.setInfoId(infoId);
		return logOperationDao.selectById(param);
	}

	public Object execute(ParamsVo<LogOperation> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<LogOperation> vo) {
		MethodParam param = setMethodParams(vo, 2, "desc");
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(" created_time desc");
		Page page = logOperationDao.pageSelect(param);
		return page;
	}

	public List<LogOperation> getList(ParamsVo<LogOperation> vo) {
		MethodParam param = setMethodParams(vo, 2, "desc");
		param.setOrderby(" created_time desc");
		return logOperationDao.select(param);
	}

	public long countInfo(ParamsVo<LogOperation> vo) {
		MethodParam param = setMethodParams(vo, 1, "");
		return logOperationDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<LogOperation> vo, int type, String orderBy) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(logOperationDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		LogOperation logOperation = vo.getObj();
		if (!Validator.isEmpty(logOperation)) {
			if (Validator.isNotNull(logOperation.getModelId())) {
				sb.append(" and modelId = :modelId");
				param.setParams("modelId", logOperation.getModelId());
			}
			if (Validator.isNotNull(logOperation.getLogType())) {
				sb.append(" and logType = :logType");
				param.setParams("logType", logOperation.getLogType());
			}
			if (Validator.isNotNull(logOperation.getCreateUser())) {
				sb.append(" and createUser = :createUser");
				param.setParams("createUser", logOperation.getCreateUser());
			}
			if (Validator.isNotNull(logOperation.getLogIp())) {
				sb.append(" and logIp = :logIp");
				param.setParams("logIp", logOperation.getLogIp());
			}
			if (Validator.isNotNull(logOperation.getOrgId())) {
				sb.append(" and orgId = :orgId");
				param.setParams("orgId", logOperation.getOrgId());
			}
			if (Validator.isNotNull(logOperation.getOrgEname())) {
				sb.append(" and orgEname = :orgEname");
				param.setParams("orgEname", logOperation.getOrgEname());
			}
			if (Validator.isNotNull(logOperation.getPrimaryKey())) {
				sb.append(" and primaryKey = :primaryKey");
				param.setParams("primaryKey", logOperation.getPrimaryKey());
			}
			if (!Validator.isEmpty(logOperation.getCreatedTime())) {
				sb.append(" and createdTime = :createdTime");
				param.setParams("createdTime", logOperation.getCreatedTime() + "");
			}
		}
		String modelSysname = String.valueOf(vo.getParams("modelSysname"));
		String modelBussiness = String.valueOf(vo.getParams("modelBussiness"));
		String modelCname = String.valueOf(vo.getParams("modelCname"));
		String createdTime = String.valueOf(vo.getParams("createdTime"));

		if (Validator.isNotNull(modelSysname)) {
			sb.append(" and modelSysname = :modelSysname");
			param.setParams("modelSysname", modelSysname);
		}
		if (Validator.isNotNull(modelBussiness)) {
			sb.append(" and modelBussiness = :modelBussiness");
			param.setParams("modelBussiness", modelBussiness);
		}
		if (Validator.isNotNull(modelCname)) {
			sb.append(" and modelCname = :modelCname");
			param.setParams("modelCname", modelCname);
		}
		if (Validator.isNotNull(createdTime)) {
			long startTime = DateUtils.strToLong(createdTime + " 00:00:00", FmtStr.yyyyMMdd_HHmmss);
			long stopTime = DateUtils.strToLong(createdTime + " 23:59:59", FmtStr.yyyyMMdd_HHmmss);
			sb.append(" and createdTime BETWEEN " + startTime + " AND " + stopTime);
			param.setParams("createdTimes", "created_Time BETWEEN " + startTime + " AND " + stopTime);
		}
		if (Validator.isNotNull(orderBy)) {
			sb.append(" order by createdTime desc");
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
